package com.yunhe.rentcars.pro.service.car.impl;

import com.yunhe.rentcars.commons.annoaction.Log;
import com.yunhe.rentcars.commons.constant.Constants;
import com.yunhe.rentcars.commons.dto.Pageable;

import com.yunhe.rentcars.pro.dao.car.CarSelfuseDao;
import com.yunhe.rentcars.pro.dao.car.CarsDao;
import com.yunhe.rentcars.pro.entity.car.CarSelfuse;
import com.yunhe.rentcars.pro.entity.rent.Cars;
import com.yunhe.rentcars.pro.service.car.CarSelfuseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author maliang
 * @version V1.0
 * @date 2017/11/25 0025 15:34
 * @Description: 车辆自用业务层
 */
@Service
public class CarSelfuseServiceImpl implements CarSelfuseService {

    @Autowired
    private CarSelfuseDao carSelfuseDao;
    @Autowired
    private CarsDao carsDao;

    @Override
    @Log(value = "添加车辆保养记录",type = 1)
    public void save(CarSelfuse carSelfuse) {
        carstate(carSelfuse);
        carSelfuseDao.save(carSelfuse);
    }

    @Override
    @Log(value = "删除保养记录",type = 2)
    public void delBatch(String ids) {
        String[] split = ids.split(",");
        carSelfuseDao.delBatch(split);
    }

    @Override
    @Log(value = "修改保养记录",type = 3)
    public void update(CarSelfuse carSelfuse) {
        carstate(carSelfuse);
        carSelfuseDao.update(carSelfuse);
    }

    @Override
    public Pageable<CarSelfuse> find(CarSelfuse carSelfuse, Integer offset, Integer limit) {
        Pageable<CarSelfuse> pageable = new Pageable<>();
        Long count = carSelfuseDao.findCount(carSelfuse);
        List<CarSelfuse> rows = null;
        if(count > 0){
            rows = carSelfuseDao.findList(carSelfuse.getCarCode(), carSelfuse.getUsecarTime(), carSelfuse.getReturnTime(), offset, limit);
        }
        pageable.setTotal(count.intValue());
        pageable.setRows(rows);
        return pageable;
    }

    @Override
    public CarSelfuse findOne(Long recordid) {
        return carSelfuseDao.findOne(recordid);
    }

    /**
     * 更改车辆状态
     * @param carSelfuse
     */
    public void carstate(CarSelfuse carSelfuse){
        Date returnTime = carSelfuse.getReturnTime();
        Cars cars = new Cars();
        cars.setCarcode(carSelfuse.getCarCode());
        if(null != returnTime) {
            carSelfuse.setStatus(Constants.CAR_NORMAL);
            cars.setCarstate(Constants.CAR_NORMAL);
        } else {
            cars.setCarstate(Constants.CAR_IN_SELFUSE);
        }
        carsDao.carState(cars);
    }
}
